package org.limit.bookServe.bean;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import static org.limit.bookServe.bean.Book.BookStatusEnum.BORROWED;
import static org.limit.bookServe.bean.Book.BookStatusEnum.NORMAL;
import static org.limit.bookServe.utils.BaseClass.println;
import static org.limit.bookServe.utils.SystemConstants.DATE_PATTERN;

/**
 * @author 1.
 * @version 1.0
 * @description:  书籍类
 * @date 2023/11/1 19:29
 */
public class Book implements Serializable {
    @Serial
    //TODO 显示声明序列化版本控件，防止 java.io.InvalidClassException 异常
    private static final long serialVersionUID = 1L;
    //唯一id
    private int bid;
    //书名
    private String bookName;
    //数量
    private int number;
    //在架状态
    private BookStatusEnum status;
    //入库时间
    private Date enterTime;
    //被借阅数
    private static final AtomicInteger borrowedNum = new AtomicInteger(0);

    public Book() {
    }

    public Book(String bookName, int number) {
        //以hash码作为唯一id
        this.bookName = bookName;
        this.number = number;
        this.status = NORMAL;
        this.enterTime = new Date(); //收录当前时间
        this.bid = hashCode();
    }


    public enum BookStatusEnum {
        NORMAL(1, "正常在架"),
        DOWN_LINE(2, "下架"),
        BORROWED(3, "被借阅")
        ;

        private int index;
        private String desc;

        BookStatusEnum(int index, String desc) {
            this.index = index;
            this.desc = desc;
        }

        public int getIndex() {
            return index;
        }

        public String getDesc() {
            return desc;
        }
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public BookStatusEnum getStatus() {
        return status;
    }

    public String getStatusDesc() {
        return status.getDesc();
    }

    public void setStatus(BookStatusEnum status) {
        this.status = status;
    }

    public Date getEnterTime() {
        return enterTime;
    }

    public void setEnterTime(Date enterTime) {
        this.enterTime = enterTime;
    }

    public int getBorrowedNum() {
        return borrowedNum.get();
    }

    public void returningBook() {
        borrowedNum.getAndDecrement();
    }

    public void setBorrowedNum() {
        borrowedNum.getAndIncrement();
    }

    //借阅书籍
    public void borrowBookFuc() {
        //修改状态
        if (this.getStatus() != BORROWED) this.setStatus(BORROWED);
        //修改书籍数目
        if (number > 0) number--;
        //记录借阅
        setBorrowedNum();
        println(bookName + "借阅成功！");
    }

    //归还书籍
    public void return2BookList() {
        //修改书籍数目
        this.returningBook();
        number++;
        //修改状态
        if (borrowedNum.get() == 0) this.setStatus(NORMAL);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return bid == book.bid && number == book.number && Objects.equals(bookName, book.bookName) && status == book.status && Objects.equals(enterTime, book.enterTime);
    }

    @Override
    public int hashCode() {
        return Objects.hash(bookName, number, status, enterTime);
    }

    @Override
    public String toString() {
        return "Book { 代码: " + this.bid + ", 书名: " + this.bookName + ", 在架数量: " + this.number + ", " +
                "当前状态: " + status.getDesc() + ", 入库时间: " + DATE_PATTERN.format(this.enterTime) + " }";
    }

    public String showCurrBookInfo() {
        return "代码: " + this.bid + ", 书名: " + this.bookName + ", 在架数量: " + this.number + ", " +
                "当前状态: " + status.getDesc() + " }";
    }
}
